package com.universal.schedule.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.universal.schedule.MainActivity;
import com.universal.schedule.R;
import com.universal.schedule.db.DatabaseHelper;
import com.universal.schedule.enity.Alarm;
import com.universal.schedule.service.AlarmServiceBroadcastReciever;
import com.universal.schedule.view.SwitchButton;

import java.util.ArrayList;
import java.util.List;

public class AlarmListAdapter extends BaseAdapter {

	private Context alarmActivity;
	private MainActivity alarmActivity1;
	private List<Alarm> alarms = new ArrayList<Alarm>();

	public static final String ALARM_FIELDS[] = { DatabaseHelper.COLUMN_ALARM_ACTIVE,
			DatabaseHelper.COLUMN_ALARM_TIME, DatabaseHelper.COLUMN_ALARM_DAYS };

	public AlarmListAdapter(Context context) {
		this.alarmActivity = context;
		alarmActivity1=(MainActivity) context;
//		Database.init(alarmActivity);
//		alarms = Database.getAll();
	}

	@Override
	public int getCount() {
		return alarms.size();
	}

	@Override
	public Object getItem(int position) {
		return alarms.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
		if (null == view)
			view = LayoutInflater.from(alarmActivity).inflate(
					R.layout.alarm_list_element, null);

		final Alarm alarm = (Alarm) getItem(position);

		SwitchButton sbButton = (SwitchButton) view.findViewById(R.id.sb_alarm);
		TextView mTvMessage = (TextView) view.findViewById(R.id.tv_message);
		mTvMessage.setText(alarm.getAlarmName());
		 sbButton.setChecked(alarm.getAlarmActive());
		 sbButton.setTag(position);
		 sbButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				alarm.setAlarmActive(isChecked);
				DatabaseHelper.update(alarm);
				callMathAlarmScheduleService();
				if (isChecked) {
					Toast.makeText(alarmActivity, alarm.getTimeUntilNextAlarmMessage(), Toast.LENGTH_LONG).show();
				}
			}
		});
		 
		TextView alarmTimeView = (TextView) view
				.findViewById(R.id.textView_alarm_time);
		alarmTimeView.setText(alarm.getAlarmTimeString());

		
			TextView alarmDaysView = (TextView) view
					.findViewById(R.id.textView_alarm_days);
			alarmDaysView.setText(alarm.getRepeatDaysString());
		

		return view;
	}

	public List<Alarm> getMathAlarms() {
		return alarms;
	}

	public void setMathAlarms(List<Alarm> alarms) {
		this.alarms = alarms;
	}
	
	protected void callMathAlarmScheduleService() {
		Intent mathAlarmServiceIntent = new Intent(alarmActivity, AlarmServiceBroadcastReciever.class);
		alarmActivity.sendBroadcast(mathAlarmServiceIntent, null);
	}

}
